<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="75">
        <el-form-item label="日期">
          <el-date-picker
            v-model="searchParam.requireDateSearch"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            @change="getrequireDate"
          />
        </el-form-item>
        <el-form-item label="产品">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入产品"
            clearable
            @keyup.enter="search"
            readonly
            @click="showWorkOrderSelect"
          >
            <template #append>
              <el-button @click="resetMaterial"> 清除 </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="searchParam.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{ textAlign: 'center' }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="70"
          key="id"
          align="center"
        />
        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          type="index"
          label="序号"
          width="70"
          key="id"
          align="center"
        />
        <el-table-column
          label="产品名称"
          prop="materialName"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ scope.row.materialName }}
          </template>
        </el-table-column>
        <el-table-column
          label="砂芯编号"
          prop="sandCoreNumber"
          min-width="100"
        />
        <el-table-column
          label="件/M"
          prop="pm"
          min-width="100"
          header-align="center"
          align="right"
        />
        <el-table-column
          label="M/S"
          prop="ms"
          min-width="100"
          header-align="center"
          align="right"
        />
        <el-table-column
          label="冷芯/热芯"
          prop="coreState"
          min-width="100"
          align="center"
        />
        <el-table-column
          label="设备型号"
          prop="equipModel"
          min-width="85"
          align="left"
        />
        <el-table-column
          label="砂型号"
          prop="sandModel"
          min-width="200"
          align="left"
        />
        <el-table-column
          label="需求人数"
          prop="demandPeople"
          min-width="85"
          header-align="center"
          align="right"
        />
        <el-table-column
          label="计划数量"
          prop="planQuantity"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="状态"
          prop="status"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.status === 1">已生成</el-tag>
            <el-tag v-else>未生成</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="日期"
          prop="requireDate"
          min-width="100"
          show-overflow-tooltip
          :formatter="formatDate"
          align="center"
        /> -->
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              :disabled="scope.row.status === 1"
              >生成制芯工单</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="saveRevoke(scope.row.id)"
              :disabled="scope.row.status === 0"
              >撤回</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mescoremonthplanactual-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <conten-select
      :visible="selectDialogSELECT.visible"
      @change-visible="changeVisibleSELECT"
      :plantId="searchParam.plantId"
      :brekList="brekList"
    />
    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESCOREMONTHPLANACTUAL",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mescoremonthplanactual/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  materialId: "",
  materialCode: "",
  materialName: "",
};

const deleteParam = {
  api: "/mes/mescoremonthplanactual/delete",
  data: {},
};
const statusList = [
  {
    id: "0",
    name: "未生成",
  },
  {
    id: "1",
    name: "已生成",
  },
];
// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mescoremonthplanactual/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开制芯月计划实际表表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "生成制芯工单";
    addDialog.data = roleObj;
    addDialog.visible = true;
    // getInfo(roleObj.id);
  } else {
    addDialog.title = "新增制芯月计划实际表";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开制芯月计划实际表表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "制芯月计划实际表详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/mes/mescoremonthplanactual/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
function getrequireDate(data: any) {
  if (data) {
    searchParam.value.requireDateStart = data[0];
    searchParam.value.requireDateEnd = data[1];
  } else {
    searchParam.value.requireDateStart = "";
    searchParam.value.requireDateEnd = "";
  }
}

//时间格式处理，yyyy-mm-dd
function formatDate(row, column) {
  const date = row[column.property];
  if (!date) return "";
  return date.split(" ")[0]; // 返回日期的年月日部分，而隐藏时间部分
}
const showWorkOrderSelect = () => {
  selectDialogSELECT.value.visible = true;
};
//选择物料弹窗
const selectDialogSELECT = ref({
  visible: false,
});
const changeVisibleSELECT = (val: boolean, data: any) => {
  selectDialogSELECT.value.visible = val;
  if (data) {
    searchParam.value.materialId = data.id;
    searchParam.value.materialCode = data.materialCode;
  }
};
// 回显的数据
const brekList = ref([]);
const resetMaterial = () => {
  searchParam.value.materialId = "";
  searchParam.value.materialCode = "";
};

async function saveRevoke(id: any) {
  ElMessageBox.confirm("是否确认撤回?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/mes/mescoremonthplanactual/saveRevoke`,
      method: "post",
      data: { id: id },
    });
    search();
  });
}

const tableColumns = reactive([
  {
    prop: "materialName",
    label: "产品名称",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "sandCoreNumber",
    label: "砂芯编号",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "pm",
    label: "件/M",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "ms",
    label: "M/S",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "coreState",
    label: "冷芯/热芯",
    minWidth: 100,
    isShow: true,
    align: "center",
  },
  {
    prop: "equipModel",
    label: "设备型号",
    minWidth: 85,
    isShow: true,
    align: "left",
  },
  {
    prop: "sandModel",
    label: "砂型号",
    minWidth: 200,
    isShow: true,
    align: "left",
  },
  {
    prop: "demandPeople",
    label: "需求人数",
    minWidth: 85,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "planQuantity",
    label: "计划数量",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "status",
    label: "状态",
    minWidth: 100,
    isShow: true,
    align: "center",
    render: (scope: any) =>
      scope.row.status === 1 ? (
        <el-tag>已生成</el-tag>
      ) : (
        <el-tag>未生成</el-tag>
      ),
  },
  {
    prop: "requireDate",
    label: "日期",
    minWidth: 100,
    isShow: true,
    align: "center",
    formatter: formatDate,
  },
]);

//  render: (scope: any) =>
//       scope.row.repairLevel == "1" && scope.row.repairLevel * 1 < 4 ? (
//         <el-tag type="danger">
//           {useDictionary("EamFaultLevel", scope.row.repairLevel)}
//         </el-tag>
//       ) : scope.row.repairStatus * 1 >= 4 ? (
//         <el-tag type="info" type="success">
//           {useDictionary("EamFaultLevel", scope.row.repairLevel)}
//         </el-tag>
//       ) : scope.row.repairLevel !== "1" && scope.row.repairStatus * 1 < 4 ? (
//         <el-tag>{useDictionary("EamFaultLevel", scope.row.repairLevel)}</el-tag>
//       ) : (
//         <div></div>
//       ),
const searchColumns = ref([]);
</script>
